<html>
<head><meta charset="utf-8"><title>Type Equality sans lifetimes constraint · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html">Type Equality sans lifetimes constraint</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209217994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Type%20Equality%20sans%20lifetimes%20constraint/near/209217994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html#209217994">(Sep 06 2020 at 09:51)</a>:</h4>
<p>Could a constraint be added  that encodes <code>&amp;'a T</code> equals <code>&amp;'b T</code>, and <code>&amp;'a &amp;'b T</code> equals <code>&amp;'b &amp;'b T</code>.<br>
Will <a href="https://github.com/rust-lang/rust/issues/20041">equality constraints in where clauses
</a> already work like this or is <code>where &amp;'a T == &amp;'b T</code> never satisfiable? </p>
<p>In other words the constraint would encode the behavior seen in the following  valid code for use in a generic context.<br>
The purpose of this constraint is to allow uses cases like <a href="#narrow/stream/144729-wg-traits/topic/Convince.20rustc.20a.20GAT.20.20only.20changes.20a.20lifetime.3F">this</a>.</p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="cp">#[derive(Eq, PartialEq)]</span><span class="w"></span>
<span class="w">    </span><span class="k">enum</span> <span class="nc">List</span><span class="o">&lt;</span><span class="na">&#39;l</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Cons</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;l</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="na">&#39;l</span><span class="w"> </span><span class="bp">Self</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="n">Nil</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="nb">Eq</span><span class="o">&gt;</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="na">&#39;b</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="nc">List</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span>::<span class="n">Cons</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">List</span>::<span class="n">Nil</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="nc">List</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span>::<span class="n">Cons</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">a</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">bar</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="nb">Eq</span><span class="o">&gt;</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="na">&#39;b</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209252552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Type%20Equality%20sans%20lifetimes%20constraint/near/209252552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html#209252552">(Sep 07 2020 at 02:42)</a>:</h4>
<p>So, I must admit I haven't really read/followed that thread</p>



<a name="209252568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Type%20Equality%20sans%20lifetimes%20constraint/near/209252568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html#209252568">(Sep 07 2020 at 02:42)</a>:</h4>
<p>But, if you're asking if you can equate two lifetimes, you can always do <code>where 'a: 'b, 'b: 'a</code></p>



<a name="209252569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Type%20Equality%20sans%20lifetimes%20constraint/near/209252569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html#209252569">(Sep 07 2020 at 02:43)</a>:</h4>
<p>But I don't think that's what you want here</p>



<a name="209254718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Type%20Equality%20sans%20lifetimes%20constraint/near/209254718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html#209254718">(Sep 07 2020 at 03:47)</a>:</h4>
<p>Quite the opposite, I need to equate types regardless of lifetimes.</p>
<p>The compiler does this, anytime you have concrete types, but this process fails when using generic types.</p>
<p>The following example is a little clearer.</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[derive(Eq, PartialEq)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">List</span><span class="o">&lt;</span><span class="na">&#39;l</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Cons</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;l</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="na">&#39;l</span><span class="w"> </span><span class="bp">Self</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Nil</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="nb">Eq</span><span class="o">&gt;</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="nc">List</span><span class="o">&lt;</span><span class="na">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Note how &#39;a and &#39;b flow into &#39;_</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="nc">List</span><span class="o">&lt;</span><span class="nb">&#39;_</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span>::<span class="n">Cons</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">bar</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="nb">Eq</span><span class="o">&gt;</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="na">&#39;b</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="nc">List</span><span class="o">&lt;&amp;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span>::<span class="n">Cons</span><span class="p">(</span><span class="o">&amp;</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">List</span>::<span class="n">Nil</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>I'm about to add a post to <a href="#narrow/stream/144729-wg-traits/topic/Convince.20rustc.20a.20GAT.20.20only.20changes.20a.20lifetime.3F">the other thread</a> using the family pattern. The contrast between concrete and generic is clear over with that example.</p>



<a name="209312324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Type%20Equality%20sans%20lifetimes%20constraint/near/209312324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html#209312324">(Sep 07 2020 at 16:34)</a>:</h4>
<p><span class="user-mention" data-user-id="232957">@Jack Huey</span> <code>'a: 'b, 'b: 'a</code> was not what I was looking for, but it made me try a totally different approach that mostly works. Thank you!</p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">CoerceLife</span><span class="o">&lt;</span><span class="na">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;a</span>: <span class="na">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="na">&#39;a</span><span class="o">&gt;</span>: <span class="na">&#39;b</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Sized</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">TypeEq</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">coerce_life</span><span class="p">(</span><span class="n">a</span>: <span class="nc">A</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209312387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Type%20Equality%20sans%20lifetimes%20constraint/near/209312387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Type.20Equality.20sans.20lifetimes.20constraint.html#209312387">(Sep 07 2020 at 16:35)</a>:</h4>
<p>Welp, glad I could help I guess! Glad you found your solution!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>